package com.lemon.tuvi2015;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;

import com.lemon.tuvi2015.adapter.VankhanAdapter;
import com.lemon.tuvi2015.object.DetailsObject;
import com.lemon.tuvi2015.object.GlobalObject;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.CMD;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;
import com.lemon.tuvi2015.view.CustomActionbar;

public class SmsListActivity extends Activity {
	CustomActionbar actionbar;
	String ID;
	GlobalObject zodiacObject;
	ArrayList<GlobalObject> listZodiac;
	ListView listView;
	ProgressBar progressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vankhan_list);
		initView();

		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(TagName.KEY))
			zodiacObject = (GlobalObject) extras.getSerializable(TagName.KEY);
		if (zodiacObject != null)
			new AsyncTaskParseJson2().execute(zodiacObject.getID());
		initActionbar();
	}

	void initView() {
		progressBar=(ProgressBar) findViewById(R.id.progressBar);
		listView = (ListView) findViewById(R.id.listView);
		listView.setEmptyView(progressBar);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				BaseEngine.startDetailsActivityObject(SmsListActivity.this, listZodiac.get(position));
				BaseEngine.startDetailsActivityObject_Key(SmsListActivity.this, listZodiac.get(position),CMD.SMSKUTE_DETAILS);
//				new AsyncTaskParseJson().execute(listZodiac.get(position).getID());
			}
		});
	}

	void initActionbar() {
		actionbar = (CustomActionbar) findViewById(R.id.header);
		OnClickListener listenerBack = new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};
		if (zodiacObject != null && !zodiacObject.getNAME().equals(""))
			actionbar.setActionbar(zodiacObject.getNAME(), listenerBack, null);
		else
			actionbar.setActionbar(R.string.home_menu_smskute, listenerBack, null);
	}
	
	public class AsyncTaskParseJson2 extends AsyncTask<String, String, String> {
	@Override
	protected void onPreExecute() {
	}

	@Override
	protected String doInBackground(String... arg0) {
		String str = JsonParser.getJson(CMD.SMSKUTE_LIST, arg0[0]);
		return str;
	}

	@Override
	protected void onPostExecute(String result) {
		if (result != null) {
			BaseEngine.showLogformLongString("", result);
			try {
				if(!JsonParser.readStatus(result).equals("")){
					listZodiac = JsonParser.readlistSms(result);
					if (listZodiac != null && listZodiac.size() > 0) {
						listView.setAdapter(new VankhanAdapter(getApplicationContext(), listZodiac,R.color.black,true));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	}

}
